package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dto.CateDTO;
import com.itheima.entity.YddCate;
import com.itheima.exception.CateNameExistException;
import com.itheima.exception.DeletionNotAllowedException;
import com.itheima.mapper.YddArticleMapper;
import com.itheima.mapper.YddCateMapper;
import com.itheima.service.YddCateService;
import com.itheima.vo.CateVO2;
import com.itheima.vo.PageInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class YddCateServiceImpl implements YddCateService {

    private final YddCateMapper yddCateMapper;

    private final YddArticleMapper yddArticleMapper;

    /**
     * 分页查询
     *
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfoVO<CateVO2> list(String catename, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<YddCate> yddCateList = yddCateMapper.selectBycatename(catename);
        List<CateVO2> cateVO2List = yddCateList.stream().map(yddCate -> {
            CateVO2 cateVO2 = yddCate.transformCateVO2();
            return cateVO2;
        }).collect(Collectors.toList());
        PageInfo<CateVO2> pageInfo = PageInfo.of(cateVO2List);

        PageInfoVO<CateVO2> pageInfoVO = new PageInfoVO<>();
        pageInfoVO.setPageSize(limit);
        pageInfoVO.setTotalPages(pageInfo.getPages());
        pageInfoVO.setCount(pageInfo.getTotal());
        pageInfoVO.setCurrentPage(pageInfo.getPageNum());
        pageInfoVO.setData(pageInfo.getList());
        return pageInfoVO;
    }

    /**
     * 分类列表全部
     *
     * @return
     */
    @Override
    public List<CateVO2> allData() {
        List<CateVO2> cateVO2List = yddCateMapper.select();
        return cateVO2List;
    }

    /**
     * 分类添加
     *
     * @param cateDTO
     */
    @Override
    public void save(CateDTO cateDTO) {
        //todo 分类名称不能一样
        Integer countCateName = yddCateMapper.selectCountByCatename(cateDTO.getCatename());
        if(countCateName > 0){
            throw new CateNameExistException(MessageConstant.REPEAT_CATE_NAME);
        }
        YddCate yddCate = cateDTO.transformYddCate();
        yddCateMapper.insert(yddCate);
    }

    /**
     * 回显
     *
     * @param id
     * @return
     */
    @Override
    public CateVO2 rowInfo(Integer id) {
        YddCate yddCate = yddCateMapper.selectById(id);
        CateVO2 cateVO2 = yddCate.transformCateVO2();
        return cateVO2;
    }

    /**
     * 修改
     *
     * @param cateDTO
     */
    @Override
    public void update(CateDTO cateDTO) {
        YddCate yddCate = cateDTO.transformYddCate();
        yddCateMapper.update(yddCate);
    }

    /**
     * 分类删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        //todo ydd_article 含有cateId,不能删除(当前分类关联了文章,不能删除)
        Integer countCateByCateId = yddArticleMapper.selectCountCateByCateId(id);
        if(countCateByCateId>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_ARTICLE);
        }
        yddCateMapper.delete(id);
    }
}
